package com.lei.dao.impl;

import com.lei.dao.RukuDao;
import com.lei.entity.Ck;
import com.lei.entity.LayDate;
import com.lei.entity.Ruku;
import com.lei.util.BaseDb;
import com.lei.util.Constants;
import com.lei.util.TransBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/22 17:12
 */
public class RukuDaoImpl implements RukuDao {
    @Override
    public int addRuku(Map map) {
        String sql = "insert into t_ruku (rukuid,rdate,uid,totalprice,totalcount,jsr) values (null,?,?,?,?,?)";
        if (map != null) {
            return BaseDb.insertAndReturnPrimaryKey(sql, Constants.getNow(), map.get("uid"), map.get("totalprice"), map.get("totalcount"), map.get("jsr"));
        }
        return -1;
    }

    @Override
    public int addRukuDetail(List<Map> tableData, int rukuid) {
        String sql = "insert into t_rukudetail (rdid,bookid,count,rukuid,pushprice) values (null,?,?,?,?)";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += BaseDb.update(sql, map.get("bookid"), map.get("count"), rukuid, map.get("pushprice"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public int rukuAndAddBookCount(List<Map> tableData) {
        String sql = "update t_book set count = count + ? where bookid = ? ";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += BaseDb.update(sql, map.get("count"), map.get("bookid"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public List<Ruku> getAllRuku(Ruku ruku) {
        StringBuilder sql = new StringBuilder("select r.rukuid,r.rdate,r.uid,r.totalprice,r.totalcount,r.jsr,s.staffName from t_ruku r join t_staff s on r.uid = s.id where 1=1");
        if (ruku.getStaffName()!=null){
            sql.append(" and s.staffName like '%").append(ruku.getStaffName()).append("%'");
        }
        if (ruku.getJsr()!=null){
            sql.append(" and r.jsr like '%").append(ruku.getJsr()).append("%'");
        }
        if (ruku.getCreatedTime()!=null){
            sql.append(" and r.rdate >= '").append(ruku.getCreatedTime()).append("'");
        }
        if (ruku.getEndTime()!=null){
            sql.append(" and r.rdate <= '").append(ruku.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc limit ?,?");
        List<Map> maps = BaseDb.query(sql + "", (ruku.getPage() - 1) * ruku.getLimit(), ruku.getLimit());
        return TransBean.populate(Ruku.class,maps);
    }

    @Override
    public long getCount(Ruku ruku) {
        StringBuilder sql = new StringBuilder("select count(*) c from t_ruku r join t_staff s on r.uid = s.id where 1=1");
        if (ruku.getStaffName()!=null){
            sql.append(" and s.staffName like '%").append(ruku.getStaffName()).append("%'");
        }
        if (ruku.getJsr()!=null){
            sql.append(" and r.jsr like '%").append(ruku.getJsr()).append("%'");
        }
        if (ruku.getCreatedTime()!=null){
            sql.append(" and r.rdate >= '").append(ruku.getCreatedTime()).append("'");
        }
        if (ruku.getEndTime()!=null){
            sql.append(" and r.rdate <= '").append(ruku.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc ");
        List<Map> maps = BaseDb.query(sql + "");
        if (maps.size()>0){
            return (long) maps.get(0).get("c");
        }
        return 0;
    }

    @Override
    public List<Map> getAllRukuDetailByRukuId(String rukuid) {
        String sql = "select r.rdid,r.rukuid,r.bookid,r.count,r.pushprice,b.bookname,b.author,b.publisher from t_rukudetail r join t_book b on r.bookid = b.bookid where r.rukuid = ?";
        return BaseDb.query(sql, rukuid);
    }

    @Override
    public int addCk(Map map) {
        String sql = "insert into t_ck(rdate,uid,totalprice,totalcount,jsr) values (now(),?,?,?,?)";
       return BaseDb.insertAndReturnPrimaryKey(sql,map.get("uid"),map.get("totalprice"),map.get("totalcount"),map.get("jsr"));
    }

    @Override
    public int addCkDetail(List<Map> tableData, int ckid) {
        String sql = "insert into t_ckdetail (rdid,bookid,count,rukuid,outprice) values (null,?,?,?,?)";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += BaseDb.update(sql, map.get("bookid"), map.get("count"), ckid, map.get("outprice"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public int ckAndUpdateBookCount(List<Map> tableData) {
        String sql = "update t_book set count = count - ? where bookid = ? ";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += BaseDb.update(sql, map.get("count"), map.get("bookid"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public long getCountCk(Ck ck) {
        StringBuilder sql = new StringBuilder("select count(*) c from t_ck r join t_staff s on r.uid = s.id where 1=1");
        if (ck.getStaffName()!=null){
            sql.append(" and s.staffName like '%").append(ck.getStaffName()).append("%'");
        }
        if (ck.getJsr()!=null){
            sql.append(" and r.jsr like '%").append(ck.getJsr()).append("%'");
        }
        if (ck.getCreatedTime()!=null){
            sql.append(" and r.rdate >= '").append(ck.getCreatedTime()).append("'");
        }
        if (ck.getEndTime()!=null){
            sql.append(" and r.rdate <= '").append(ck.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc ");
        List<Map> maps = BaseDb.query(sql + "");
        if (maps.size()>0){
            return (long) maps.get(0).get("c");
        }
        return 0;
    }

    @Override
    public List<Ck> getAllCk(Ck ck) {
        StringBuilder sql = new StringBuilder("select r.rukuid,r.rdate,r.uid,r.totalprice,r.totalcount,r.jsr,s.staffName from t_ck r join t_staff s on r.uid = s.id where 1=1");
        if (ck.getStaffName()!=null){
            sql.append(" and s.staffName like '%").append(ck.getStaffName()).append("%'");
        }
        if (ck.getJsr()!=null){
            sql.append(" and r.jsr like '%").append(ck.getJsr()).append("%'");
        }
        if (ck.getCreatedTime()!=null){
            sql.append(" and r.rdate >= '").append(ck.getCreatedTime()).append("'");
        }
        if (ck.getEndTime()!=null){
            sql.append(" and r.rdate <= '").append(ck.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc limit ?,?");
        List<Map> maps = BaseDb.query(sql + "", (ck.getPage() - 1) * ck.getLimit(), ck.getLimit());
        return TransBean.populate(Ck.class,maps);
    }

    @Override
    public List<Map> getAllCkDetailByCkId(String ckid) {
        String sql = "select r.rdid,r.rukuid,r.bookid,r.count,r.outprice,b.bookname,b.author,b.publisher from t_ckdetail r join t_book b on r.bookid = b.bookid where r.rukuid = ?";
        return BaseDb.query(sql, ckid);
    }

    @Override
    public Map<String, List> getCkAndRuKuMoney(LayDate layDate) {
        // 定义默认起始月
        int minMonth = 1;
        // 定义默认结束月
        int maxMonth = 12;

        // 拼接查询入库金额的sql
        String sql = "SELECT MONTH(rdate) month,SUM( totalprice ) money FROM t_ruku tr WHERE 1=1 ";
        if (layDate.getYear()!=null){
            sql+=" and year(rdate) = "+layDate.getYear();
        }else{
            sql+=" and year(rdate) = 2020";
        }
        if (layDate.getMonthMin()!=null){
            sql+=" and month(rdate) >="+layDate.getMonthMin();
            minMonth=Integer.parseInt(layDate.getMonthMin());
        }else {
            sql+=" and month(rdate) >= 1";
        }
        if (layDate.getMonthMax()!=null){
            sql+=" and month(rdate) <="+layDate.getMonthMax();
            maxMonth = Integer.parseInt(layDate.getMonthMax());
        }else {
            sql+=" and month(rdate) <= 12";
        }
        sql+=" group by month(rdate)";

        // 拼接查询出库金额的SQL
        String sql2 = "SELECT MONTH(rdate) month,SUM( totalprice ) money FROM t_ck tr WHERE 1=1 ";
        if (layDate.getYear()!=null){
            sql2+=" and year(rdate) = "+layDate.getYear();
        }else{
            sql2+=" and year(rdate) = 2020";
        }
        if (layDate.getMonthMin()!=null){
            sql2+=" and month(rdate) >="+layDate.getMonthMin();
        }else {
            sql2+=" and month(rdate) >= 1";
        }
        if (layDate.getMonthMax()!=null){
            sql2+=" and month(rdate) <="+layDate.getMonthMax();
        }else {
            sql2+=" and month(rdate) <= 12";
        }
        sql2+=" group by month(rdate)";

        // 查询入库金额的结果
        List<Map> list = BaseDb.query(sql);

        // 查询出库金额的结果
        List<Map> list2 = BaseDb.query(sql2);

        // 定义一个Map 装载 处理后的数据，页面要的数据类型
        Map<String, List> mapList = new HashMap<>(64);

        // 装填月份的list
        List<Object> monthData = new ArrayList<>();

        // 装填入库金额的list
        List<Double> ruKuMoney = new ArrayList<>();

        // 装填出库金额的list
        List<Double> ckMoney = new ArrayList<>();

        //月份的循环
        for (int i = minMonth; i <= maxMonth; i++) {
            double value = 0;
            double value2 = 0;
            monthData.add(i+"");
            for (Map map : list) {
                // 如果 查到的map中有相同的月份，则将对应的钱数存到ruKuMoney对应的月份的位置
                if (Integer.parseInt(map.get("month")+"")==i){
                    value =Double.parseDouble(map.get("money")+"") ;
                }
            }
            for (Map map : list2) {
                // 如果 查到的map中有相同的月份，则将对应的钱数存到ckMoney对应的月份的位置
                if (Integer.parseInt(map.get("month")+"")==i){
                    value2 =Double.parseDouble(map.get("money")+"") ;
                }
            }
            // 如果 查到的map中没有相同的月份，0 对应的月份的位置
            ruKuMoney.add(value);
            ckMoney.add(value2);
        }
        mapList.put("ruKuMoney",ruKuMoney);
        mapList.put("ckMoney",ckMoney);
        mapList.put("monthData",monthData);
        return mapList;
    }

}
